<?php

namespace App\Repository\Service;

use App\Models\Service\ServiceCallModel;
use App\Models\Service\ServiceCallRemarkModel;
use App\Repository\BaseRepository;

class ServiceCallRemarkRepository extends BaseRepository
{
    public $model;
    public $log;

    public function __construct()
    {
        $this->model = new ServiceCallRemarkModel();
//        $this->log = new Logger('ServiceCall');
//        $this->log->pushHandler(
//            (new RotatingFileHandler(storage_path('/logs/ServiceCall.log'), 5, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
//        );
        parent::__construct();
    }

    //列表
    public function index($request, $page = 1, $limit = 10)
    {

    }

//外呼处显示的历史备注
    public function callTaskRemarkList($params, $page = 1, $limit = 10)
    {
        $orders_id = $params['orders_id'];
        $fields = ['a.nickname', 'a.created_at', 'a.record', 'a.talk_time', 'a.talk_type', 'a.link_type', 'b.remark', 'b.label_name', 'b.tagging'];
        $where = [];
        $where['eq']['a.orders_id'] = $orders_id;
//        $where['egt']['a.call_task_id'] = 1;
        $total = 0;
        $ModelServicCall = new ServiceCallModel();
        if ($page <= 1 && (isset($params['isListLogs']) && $params['isListLogs'] == 1)) {
            $total = $ModelServicCall->from($ModelServicCall->getTable(), 'a')->searchWhere($where)->count('a.id');
        }
        $rows = $ModelServicCall->from($ModelServicCall->getTable(), 'a')
            ->leftJoin($this->model->getTable() . ' AS b', 'a.md5sum', '=', 'b.md5sum')
            ->select($fields)
            ->orderByDesc('a.id')
            ->searchWhere($where)
            ->page($page, $limit)
            ->get();
        return $this->returnMsg(200, '查询成功', $rows, $total);
    }

//记录
    public function add($params)
    {
        $optionalFields = $this->model->getTableField();
//        $mustField = [
//            'name' => '类型名字不能为空'
//        ];
        $insertData = $this->getRealData($params, [], $optionalFields);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }
        $insertData['md5sum'] = md5($params['md5str']);
        $insertData['users_id'] = $params['user']['id'];
        if ($this->model->where(['md5sum' => $insertData['md5sum']])->count('id') > 0) {
            return $this->returnMsg(1, '该记录已存在');
        }
        $res = $this->model->insert($insertData);
        if ($res) {
            return $this->returnMsg(200, '添加成功');
        }
        return $this->returnMsg(1, '添加失败');
    }
}
